package com.dmap.run.service.impl;

import com.dmap.run.controller.api.param.SwimRecordParam;
import com.dmap.run.dao.RunRecordMapper;
import com.dmap.run.dao.SwimRecordMapper;
import com.dmap.run.po.RunRecord;
import com.dmap.run.po.RunRecordExample;
import com.dmap.run.po.SwimRecord;
import com.dmap.run.po.SwimRecordExample;
import com.dmap.run.service.SwimRecordService;
import com.dmap.run.vo.SwimRecordHistoryVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 08/06/18 Time: 下午 12:02
 * \* Description:
 * \
 */
@Service
public class SwimRecordServiceImpl implements SwimRecordService {

    @Resource
    private SwimRecordMapper swimRecordMapper;

    @Override
    public boolean insertSwimRecord(SwimRecordParam param) {
        SwimRecord swimRecord = new SwimRecord();
        swimRecord.setUserId(param.getUserId());
        swimRecord.setDataSource(param.getDataSource());
        swimRecord.setStartTime(param.getStartTime());
        swimRecord.setEndTime(param.getEndTime());
        swimRecord.setCalorie(param.getCalorie());
        swimRecord.setTimeTotal(param.getTimeTotal());
        swimRecord.setStrokeNumber(param.getStrokeNumber());
        swimRecord.setStrokeRate(param.getStrokeRate());
        swimRecord.setCreateTime(System.currentTimeMillis() / 1000);
        swimRecord.setUpdateTime(System.currentTimeMillis() / 1000);
        return swimRecordMapper.insertSelective(swimRecord) == 1;
    }

    @Override
    public boolean isExistSwimRecord(long userId, long startTime, long endTime) {
        SwimRecordExample example = new SwimRecordExample();
        example.createCriteria().andUserIdEqualTo(userId).andStartTimeEqualTo(startTime).andEndTimeEqualTo(endTime);
        return CollectionUtils.isNotEmpty(swimRecordMapper.selectByExample(example));
    }

    @Override
    public SwimRecordHistoryVo selectSwimRecordHistoryList(long userId) {
        SwimRecordHistoryVo swimRecordHistoryVo = new SwimRecordHistoryVo();
        SwimRecordExample example = new SwimRecordExample();
        example.createCriteria().andUserIdEqualTo(userId);
        int count = swimRecordMapper.countByExample(example);
        if (count == 0) {
            swimRecordHistoryVo.setSwimRecordSimpleList(Collections.emptyList());
            swimRecordHistoryVo.setSwimNum(0);
            swimRecordHistoryVo.setTimeTotal(0);
            swimRecordHistoryVo.setCalorie(0);
            swimRecordHistoryVo.setStrokeNumber(0);
        }
        List<SwimRecord> swimRecordList = swimRecordMapper.selectByExample(example);
        int timeTotal = 0;
        int calorie = 0;
        int strokeNumber = 0;
        for (SwimRecord record : swimRecordList) {
            timeTotal = timeTotal + record.getTimeTotal();
            calorie = calorie + record.getCalorie();
            strokeNumber = strokeNumber + record.getStrokeNumber();
        }
        swimRecordHistoryVo.setSwimNum(count);
        swimRecordHistoryVo.setTimeTotal(timeTotal);
        swimRecordHistoryVo.setCalorie(calorie);
        swimRecordHistoryVo.setStrokeNumber(strokeNumber);
        return swimRecordHistoryVo;
    }

    @Override
    public List<SwimRecord> selectSwimRecordListByPage(long userId, int pageIndex, int pageSize) {
        SwimRecordExample example = new SwimRecordExample();
        example.setLimitStart((pageIndex - 1) * pageSize);
        example.setLimitEnd(pageSize);
        example.setOrderByClause("start_time desc");
        example.createCriteria().andUserIdEqualTo(userId);
        List<SwimRecord> swimRecordList = swimRecordMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(swimRecordList) ? swimRecordList : Collections.emptyList();
    }

    @Override
    public int selectUserTotalTime(long userId) {

        SwimRecordExample example = new SwimRecordExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<SwimRecord> swimRecordList = swimRecordMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(swimRecordList)) {
            int totalTime = 0;
            for (SwimRecord record : swimRecordList) {
                totalTime = totalTime + record.getTimeTotal();
            }
            return totalTime;
        }
        return 0;
    }

    @Override
    public SwimRecord selectSwimRecordById(long swimRecordId) {
        return swimRecordMapper.selectByPrimaryKey(swimRecordId);
    }
}
